﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MillerApp.DataAccess.MillerDB;
using MillerApp.DataContract.MillerDB;
using MillerApp.UserControls;
using MillerApp.UserControls.Enums;

namespace MillerApp.Stock
{
    public partial class StockInfoQueryForm : FormBase
    {
        public StockInfoQueryForm()
        {
            InitializeComponent();
        }

        string column = string.Empty;
        SortDirection direction = SortDirection.ASC;

        private void StockInfoQueryForm_Load(object sender, EventArgs e)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.queryActions1.PagerControl = this.dataPagerControl1;
            this.queryActions1.OnQueryingData += new UserControls.Delegate.QueryingDataEventHandler(queryActions1_OnQueryingData);
            this.queryActions1.GetConditions += new UserControls.Delegate.GetStringDelegate(queryActions1_GetConditions);
            this.queryActions1.GetSortExpression += new UserControls.Delegate.GetSortExpression(queryActions1_GetSortExpression);
            dataGridView1.ColumnHeaderMouseClick += delegate(object sender2, DataGridViewCellMouseEventArgs e2)
            {
                column = this.queryActions1.CurrentSortColumn;
                direction = this.queryActions1.CurrentSortDirection;
                string columnName = dataGridView1.Columns[e2.ColumnIndex].Name;
                if (columnName.IndexOf("GRD_EXP") >= 0)
                {
                    return;
                }
                ////retail_EX//allflow_EX//inflowmain_EX
                if (columnName == "inflowmain_EX")
                {
                    columnName = "(inflowmain-outflowmain)";
                }
                if (columnName == "retail_EX")
                {
                    columnName = "(inflowretail-outflowretail)";
                }
                if (columnName == "allflow_EX")
                {
                    columnName = "(inflowmain-outflowmain+inflowmain-outflowmain)";
                }

                if (columnName == column)
                {
                    direction = direction == SortDirection.ASC ? SortDirection.DESC : SortDirection.ASC;
                }
                else
                {
                    column = columnName;
                }
                this.queryActions1.OnQueryData();
            };
        }

        SortExpression queryActions1_GetSortExpression()
        {
            SortExpression exp = new SortExpression();
            exp.SortColumn = column;
            exp.SortDirection = direction;
            return exp;
        }

        string queryActions1_GetConditions()
        {
            string conditions = string.Empty;
            if (string.IsNullOrEmpty(txtTypeName.Text) == false)
            {
                conditions += " and secCode like '%" + txtTypeName.Text + "%'";
            }
            if (string.IsNullOrEmpty(this.dpkerStart.Text) == false)
            {
                conditions += " and tradeday>= '" + dpkerStart.Value.Date.ToString("yyyy-MM-dd") + "'";
            }
            if (string.IsNullOrEmpty(this.dpkerEnd.Text) == false)
            {
                conditions += " and tradeday<= '" + dpkerEnd.Value.Date.ToString("yyyy-MM-dd") + "'";
            }
            return conditions;
        }

        void queryActions1_OnQueryingData(string conditions, string sortExpression, int pageIndex, int pageSize)
        {
            int recordCount = 0;
            List<StockquotationsEntity> list = StockquotationsManager.GetStockquotationssByCond(conditions, sortExpression, pageIndex, pageSize, out recordCount);
            this.dataGridView1.DataSource = list;
            this.queryActions1.RecorderCount = recordCount;
            SetDataGridForeColor(dataGridView1);
        }

        private   void SetDataGridForeColor(DataGridView gridView)
        {
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                List<int> setRedCells = new List<int>();
                object obj = gridView.Rows[i].DataBoundItem;
                if(obj==null)
                {
                    continue;
                }
                StockquotationsEntity dataItem = obj as StockquotationsEntity;
                if (dataItem == null)
                {
                    continue;
                }
                if (dataItem.mainflow > 0)
                {
                    SetCellForeColor(gridView,i, (int)ColumnIndex.MainFlow, Color.Red);
                }
                else
                {
                    SetCellForeColor(gridView,i, (int)ColumnIndex.MainFlow, Color.Green);
                }
                if (dataItem.retailflow > 0)
                {
                    SetCellForeColor(gridView,i, (int)ColumnIndex.RetailFlow, Color.Red);
                }
                else
                {
                    SetCellForeColor(gridView,i, (int)ColumnIndex.RetailFlow, Color.Green);
                }
                if (dataItem.allflow > 0)
                {
                    SetCellForeColor(gridView,i, (int)ColumnIndex.AllFlow, Color.Red);
                }
                else
                {
                    SetCellForeColor(gridView,i, (int)ColumnIndex.AllFlow, Color.Green);
                }
                if (dataItem.changeratio > 0)
                {
                    SetCellForeColor(gridView,i, (int)ColumnIndex.ChangeRatio, Color.Red);
                }
                else
                {
                    SetCellForeColor(gridView,i, (int)ColumnIndex.ChangeRatio, Color.Green);
                }
                if (dataItem.todayclosingprice-dataItem.yesterdayclosingprice>0)
                {
                    SetCellForeColor(gridView,i, (int)ColumnIndex.Price, Color.Red);
                }
                else
                {
                    SetCellForeColor(gridView,i, (int)ColumnIndex.Price, Color.Green);
                }
            }
        }

        private void SetCellForeColor(DataGridView gridView, int row, int cell, Color color)
        {
            gridView.Rows[row].Cells[cell].Style.ForeColor = color;
        }

        private enum ColumnIndex
        {
            MainFlow=2,
            RetailFlow=3,
            AllFlow=4,
            ChangeRatio=5,
            Price=6,
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //显示点击的是哪一行那一列
            Console.WriteLine("" + e.RowIndex + "Message" + e.ColumnIndex);
            //显示点击的哪一行的内容.
            DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            Console.WriteLine(cell.Value);
        }

    }
}
